/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.example.simple;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.client.hook.SendMessageHook;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Producer {
    public static void main(String[] args) throws MQClientException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
        //producer.setNamesrvAddr("10.30.30.64:9876;10.30.151.209:9876;10.30.176.225:9876;10.30.30.67:9876");
        //producer.setNamesrvAddr("10.30.130.105:9876");
        producer.setNamesrvAddr("127.0.0.1:9876");
        //开启发送消息时，启用Broker故障延迟机制
        //producer.setSendLatencyFaultEnable(true);
        
        // 注册SendMessageHook
        producer.getDefaultMQProducerImpl().registerSendMessageHook(new ProducerTestHook());
        
        producer.start();

        //生产顺序消息
        /*producer.send(message, new MessageQueueSelector() {
            @Override
            public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                long orderId = (Long) arg;
                int index = (int) (orderId % mqs.size());
                return mqs.get(index);
            }
        }, order.getOrderId());*/
        
        for (int i = 0; i < 1; i++)
            try {
                {
                    Message msg = new Message("test_topic1",
                        "TagA",
                        "OrderID188",
                        "Hello world1".getBytes(RemotingHelper.DEFAULT_CHARSET));
                    SendResult sendResult = producer.send(msg);
                    //producer.sendOneway();
                    //producer.send();
                    System.out.printf("%s%n", sendResult);
                    
                    /*Message msg2 = new Message("test_topic2",
                            "TagA",
                            "OrderID188",
                            "Hello world2".getBytes(RemotingHelper.DEFAULT_CHARSET));
                    SendResult send = producer.send(msg2);
                    System.out.println(send);*/
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        producer.shutdown();
    }
    
    /**
     * 自定义生产者Hook函数
     */
    public static class ProducerTestHook implements SendMessageHook {
        public static final Logger LOGGER = LoggerFactory.getLogger(ProducerTestHook.class);
        
        @Override
        public String hookName() {
            return ProducerTestHook.class.getName();
        }
        @Override
        public void sendMessageBefore(SendMessageContext sendMessageContext) {
            LOGGER.info("execute sendMessageBefore. sendMessageContext:{}", sendMessageContext);
        }
        @Override
        public void sendMessageAfter(SendMessageContext sendMessageContext) {
            LOGGER.info("execute sendMessageAfter. sendMessageContext:{}", sendMessageContext);
        }
    }
}
